package org.jeecg.modules.rental.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.rental.cache.ScenicSpotCache;
import org.jeecg.modules.rental.entity.RentalEBike;
import org.jeecg.modules.rental.entity.RentalScenicSpot;
import org.jeecg.modules.rental.service.IRentalScenicSpotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 区域景点
 * @Author: sean
 * @Date:   2024-07-13
 * @Version: V1.0
 */
@Api(tags="区域景点")
@RestController
@RequestMapping("/rental/scenicspot")
@Slf4j
public class RentalScenicSpotController extends JeecgController<RentalScenicSpot, IRentalScenicSpotService> {
	@Autowired
	private IRentalScenicSpotService rentalScenicSpotService;

	@Autowired
	private ScenicSpotCache scenicSpotCache;

	/**
	 * 分页列表查询
	 *
	 * @param rentalScenicSpot
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	//@AutoLog(value = "区域景点-分页列表查询")
	@ApiOperation(value="区域景点-分页列表查询", notes="区域景点-分页列表查询")
	@GetMapping(value = "/list")
	@PermissionData(pageComponent="rental/near/spotlist")
	public Result<IPage<RentalScenicSpot>> queryPageList(RentalScenicSpot rentalScenicSpot, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
														 @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
														 HttpServletRequest request) {
		QueryWrapper<RentalScenicSpot> queryWrapper = QueryGenerator.initQueryWrapper(rentalScenicSpot, request.getParameterMap());
		Page<RentalScenicSpot> page = new Page<>(pageNo, pageSize);
		IPage<RentalScenicSpot> pageList = rentalScenicSpotService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 /**
	  * 分页列表查询
	  *
	  * @param rentalScenicSpot
	  * @return
	  */
	 //@AutoLog(value = "区域景点-分页列表查询")
	 @ApiOperation(value="区域景点-选项列表查询", notes="区域景点-选项列表查询")
	 @GetMapping(value = "/options")
	 public Result<List<RentalScenicSpot>> options(RentalScenicSpot rentalScenicSpot) {
		 List<RentalScenicSpot> optionList = rentalScenicSpotService.selectOptions(rentalScenicSpot);
		 return Result.OK(optionList);
	 }

	
	/**
	 *   添加
	 *
	 * @param rentalScenicSpot
	 * @return
	 */
	@AutoLog(value = "区域景点-添加")
	@ApiOperation(value="区域景点-添加", notes="区域景点-添加")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody RentalScenicSpot rentalScenicSpot) {
		rentalScenicSpotService.add(rentalScenicSpot);
		return Result.OK("common.add.success");
	}
	
	/**
	 *  编辑
	 *
	 * @param rentalScenicSpot
	 * @return
	 */
	@AutoLog(value = "区域景点-编辑")
	@ApiOperation(value="区域景点-编辑", notes="区域景点-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody RentalScenicSpot rentalScenicSpot) {
		rentalScenicSpotService.edit(rentalScenicSpot);
		return Result.OK("common.edit.success");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "区域景点-通过id删除")
	@ApiOperation(value="区域景点-通过id删除", notes="区域景点-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		scenicSpotCache.removeLocation(id);
		rentalScenicSpotService.removeById(id);
		return Result.OK("common.delete.success");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "区域景点-批量删除")
	@ApiOperation(value="区域景点-批量删除", notes="区域景点-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		String[] idStr = ids.split(",");
		scenicSpotCache.removeLocation(idStr);
		this.rentalScenicSpotService.removeByIds(Arrays.asList(idStr));
		return Result.OK("common.delete.batch.success");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "区域景点-通过id查询")
	@ApiOperation(value="区域景点-通过id查询", notes="区域景点-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<RentalScenicSpot> queryById(@RequestParam(name="id",required=true) String id) {
		RentalScenicSpot rentalScenicSpot = rentalScenicSpotService.getById(id);
		if(rentalScenicSpot==null) {
			return Result.error("common.query.not.found",null);
		}
		return Result.OK(rentalScenicSpot);
	}

	/**
	 * 通过经纬度查询当前推荐路线
	 * @param latitude
	 * @param longitude
	 * @return
	 */
	@ApiOperation(value="景点线路规划-通过id查询", notes="景点线路规划-通过id查询")
	@GetMapping(value = "/query-by-Location")
	public Result<List<RentalScenicSpot>> queryByLocation(@RequestParam(name="latitude",required=true) String latitude,
															   @RequestParam(name="longitude",required=true) String longitude) {
		List<RentalScenicSpot> scenicSpotList = rentalScenicSpotService.queryByLocation(latitude, longitude);
		return Result.OK(scenicSpotList);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param rentalScenicSpot
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, RentalScenicSpot rentalScenicSpot) {
        return super.exportXls(request, rentalScenicSpot, RentalScenicSpot.class, "区域景点");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, RentalScenicSpot.class);
    }

}
